package com.wx.tnsadmin.utils;


import com.wx.tnsadmin.model.UmsAdminModel;
import com.wx.tnsadmin.model.UmsAuthModel;
import com.wx.tnsadmin.model.UmsRoleModel;
import com.wx.tnsadmin.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("userDetailsService")
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UmsAdminService umsAdminService;


    @Override
    public UserDetails loadUserByUsername ( String s ) throws UsernameNotFoundException {
        UmsAdminModel admin = umsAdminService.findByUsername ( s );
        if (admin == null) {
            throw new UsernameNotFoundException ( "账号不存在!" );
        }

        List<UmsAuthModel> umsAuthModels = umsAdminService.getAuthList ( admin.getUsername ( ) );
        return new AdminUserDetails ( admin,umsAuthModels );
    }
}